package com.hboxs.asl.entity;

import com.fasterxml.jackson.annotation.JsonValue;
import com.hboxs.asl.qcloudLVB.entity.Live;
import com.hboxs.asl.service.CourseTradeService;
import com.hboxs.asl.util.DateUtils;
import org.hibernate.search.annotations.*;
import org.wltea.analyzer.lucene.IKSimilarity;

import javax.annotation.Resource;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.*;

/**
 * Entity - 课程
 */
@Indexed
@Similarity(impl = IKSimilarity.class)
@Entity
@Table(name = "xx_course")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_course_sequence")
public class Course extends BaseEntity {

    private static final long serialVersionUID = 1475770987671585482L;
    @Resource(name = "courseTradeService")
    private CourseTradeService courseTradeService;

    public enum Type {

        /**
         * 预告
         */
        trailer,

        /**
         * 课程
         */
        course;

        private static Map<String, Course.Type> map = new HashMap<>();

        static {
            map.put("预告", trailer);
            map.put("课程", course);
        }

        public static Course.Type forValue(String value) {
            return map.get(value);
        }

        @JsonValue
        public String toValue() {
            for (Map.Entry<String, Course.Type> entry : map.entrySet()) {
                if (entry.getValue() == this) {
                    return entry.getKey();
                }
            }

            return "";
        }

        public static Course.Type getDefault() {
            return Course.Type.trailer;
        }
    }

    public enum CourseType {

        /**
         * 未直播
         */
        nolive,

        /**
         * 直播中
         */
        live,

        /**
         * 录播
         */
        finished;

        private static Map<String, Course.CourseType> map = new HashMap<>();

        static {
            map.put("未直播", nolive);
            map.put("直播中", live);
            map.put("录播", finished);
        }

        public static Course.CourseType forValue(String value) {
            return map.get(value);
        }

        @JsonValue
        public String toValue() {
            for (Map.Entry<String, Course.CourseType> entry : map.entrySet()) {
                if (entry.getValue() == this) {
                    return entry.getKey();
                }
            }

            return "";
        }

        public static Course.CourseType getDefault() {
            return Course.CourseType.nolive;
        }
    }

    /** 标题 */
    private String title;

    /** 预告图片URL*/
    private String imageURL;

    /** 简介 */
    private String introduction;

    /** 是否收费*/
    private Boolean charge;

    /** 是否加密*/
    private Boolean encrypt;

    /** 是否设置分享密码 */
    private String password;

    /** 发布日期 */
    private Long pubdate;

    /** 日期转换 */
    public Date datePattern;

    /** 价格 */
    private BigDecimal price = BigDecimal.ZERO;

    /** 类型 */
    private Type type;

    /** 状态 */
    private CourseType courseType;

    /** 分类 */
    private Category category;

    /** 直播 */
    private Live live;

    /** 发布人 */
    private Member member;

    /** 评价 */
    private Set<Review> reviews = new HashSet<>();

    /** 报名人数 */
    private Long applyCount = 0l;

    /** 是否置顶 */
    private Boolean isTop;

    /** 热门搜索 */
    private Boolean hotSearch;

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public CourseType getCourseType() {
        return courseType;
    }

    public void setCourseType(CourseType courseType) {
        this.courseType = courseType;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    @OneToOne(fetch = FetchType.LAZY)
    public Live getLive() {
        return live;
    }

    public void setLive(Live live) {
        this.live = live;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImageURL() {
        return imageURL;
    }

    public void setImageURL(String imageURL) {
        this.imageURL = imageURL;
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public Boolean getCharge() {
        return charge;
    }

    public void setCharge(Boolean charge) {
        this.charge = charge;
    }

    public Boolean getEncrypt() {
        return encrypt;
    }

    public void setEncrypt(Boolean encrypt) {
        this.encrypt = encrypt;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    @OneToMany(mappedBy = "course", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    public Set<Review> getReviews() {
        return reviews;
    }

    public void setReviews(Set<Review> reviews) {
        this.reviews = reviews;
    }

    public Long getPubdate() {
        return pubdate;
    }

    public void setPubdate(Long pubdate) {
        this.pubdate = pubdate;
    }

    public Date getDatePattern() {
        return datePattern;
    }

    public void setDatePattern(Date datePattern) {
        this.datePattern = datePattern;
    }

    public Long getApplyCount() {
        return applyCount;
    }

    public void setApplyCount(Long applyCount) {
        this.applyCount = applyCount;
    }

    @Transient
    public String getCreateDateF(){
        return DateUtils.date2Str(super.getCreateDate());
    }

    /**
     * 获取是否置顶
     *
     * @return 是否置顶
     */
    @Field(store = Store.YES, index = Index.UN_TOKENIZED)
    @NotNull
    @Column(nullable = false)
    public Boolean getIsTop() {
        return isTop;
    }

    /**
     * 设置是否置顶
     *
     * @param isTop 是否置顶
     */
    public void setIsTop(Boolean isTop) {
        this.isTop = isTop;
    }

    /**
     * 获取是否热门搜索
     *
     * @return 是否热门搜索
     */
    @Field(store = Store.YES, index = Index.UN_TOKENIZED)
    @NotNull
    @Column(nullable = false)
    public Boolean getHotSearch() {
        return hotSearch;
    }

    public void setHotSearch(Boolean hotSearch) {
        this.hotSearch = hotSearch;
    }
}